package com.ciwong.epaper.modules.wordlist.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.wordlist.bean.PageListBean;
import com.ciwong.mobilelib.utils.ToastUtil;

import java.util.ArrayList;


/**
 * Created by Administrator on 2018/3/20.
 */

public class WorkBookAdapter extends RecyclerView.Adapter {


    private Context context;
    private ArrayList<PageListBean> listWordBook;
    private LayoutInflater inflater;
    private OnItemClickListener onItemClickListener;
    private OnItemLongClickListener onItemLongClickListener;
    private boolean checkWordIconVisiable = false;

    public void setData(ArrayList<PageListBean> pageListNet) {
        listWordBook = pageListNet;
        notifyDataSetChanged();
    }

    public void addNewData(ArrayList<PageListBean> pageListNet) {
        if (listWordBook != null)
            listWordBook.addAll( pageListNet);
        else
            listWordBook = pageListNet;
        notifyDataSetChanged();
    }

    public ArrayList<PageListBean> getData() {
        return this.listWordBook;
    }

    public interface OnItemClickListener {
        void onItemClick(WorkBookHolder holder, PageListBean workBookItemBean, int position);

        void onItemCheck(WorkBookHolder holder, PageListBean workBookItemBean, int position);
    }

    public void setOnClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;

    }

    public interface OnItemLongClickListener {
        boolean onItemLongClick(boolean checkBoxVisiable);
    }

    public void setOnLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    public void setCheckWordIconVisiable(boolean checkWordIconVisiable) {
        this.checkWordIconVisiable = checkWordIconVisiable;
        notifyDataSetChanged();
    }

    public boolean getCheckWordIconVisiable() {
        return this.checkWordIconVisiable;
    }

    public WorkBookAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = inflater.inflate(R.layout.item_workbook, parent, false);
        return new WorkBookHolder(inflate);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final WorkBookHolder holder1 = (WorkBookHolder) holder;
        final PageListBean pageItemBean = listWordBook.get(position);
        holder1.tvWord.setText(pageItemBean.getContent());
        if (checkWordIconVisiable) {
            holder1.tvWord.setBackgroundResource(R.drawable.item_bg_workbook1);
            holder1.ivCheck.setVisibility(View.VISIBLE);
            if (pageItemBean.getSelected()) {
                holder1.ivCheck.setImageResource(R.mipmap.word_checked);
                holder1.tvWord.setTextColor(context.getResources().getColor(R.color.color_353c38));
            } else {
                holder1.tvWord.setTextColor(context.getResources().getColor(R.color.item_nomal));
                holder1.ivCheck.setImageResource(R.mipmap.word_unchecked);
            }
        } else {
            holder1.tvWord.setTextColor(context.getResources().getColorStateList(R.color.check_textview));
            holder1.tvWord.setBackgroundResource(R.drawable.item_bg_workbook);
            holder1.ivCheck.setVisibility(View.GONE);
        }

        holder1.ivCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (onItemClickListener != null) {
                    onItemClickListener.onItemCheck(holder1, pageItemBean, position);
                }
            }
        });
        holder1.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkWordIconVisiable) {//在条目可选择状态下，让条目只改变状态
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemCheck(holder1, pageItemBean, position);
                    }
                    return;
                }
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(holder1, pageItemBean, position);
                }

            }
        });

    }

    @Override
    public int getItemCount() {
        return listWordBook == null ? 0 : listWordBook.size();
    }


    public class WorkBookHolder extends RecyclerView.ViewHolder {

        private View itemView;
        public ImageView ivCheck;
        private TextView tvWord;

        public WorkBookHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            ivCheck = itemView.findViewById(R.id.iv_check);
            tvWord = itemView.findViewById(R.id.tv_word);
        }
    }
}
